<template>
  <div>
    <div>
      <div>&nbsp;</div>
      <div>&nbsp;</div>
      <div>&nbsp;</div>
      <div>&nbsp;</div>
      <van-cell-group>
        <van-cell title="订单日期:" :value="so.date_order" />
        <van-cell title="订单编号:" :value="so.name" />
        <van-cell title="大夫:" :value="so.analytic_account_partner_id__name" />
        <van-cell title="咨询费:" :value="so.amount_total" />
        <van-cell title="订单状态:" :value="so.state_name" />
        <van-cell title="开票状态:" :value="so.invoice_status_name" />
        <van-cell title="发票编号:" :value="invoice.name" />
        <van-cell title="发票状态:" :value="invoice.state_name" />
        <van-cell
          title="付款状态:"
          :value="invoice.invoice_payment_state_name"
        />
      </van-cell-group>

      <div v-if="isCustomer">
        <van-button
          v-if="so.state === 'draft'"
          size="small"
          @click.native="handleSoConfirm"
        >
          订单确认
        </van-button>

        <van-button
          v-if="so.invoice_status === 'to invoice'"
          size="small"
          @click.native="handleSoToInvoice"
        >
          订单开票
        </van-button>

        <van-button
          v-if="isCustomer && invoice.state === 'draft'"
          size="small"
          @click.native="handleInvoicePost"
        >
          发票确认
        </van-button>

        <van-button
          v-if="isCustomer && isInvoiceToPay"
          size="small"
          @click.native="handleInvoicePay"
        >
          支付
        </van-button>
      </div>

      <van-button
        v-if="isEmployee && isSoToDone"
        size="small"
        @click.native="handleSoDone"
      >
        完成咨询
      </van-button>

      <!-- v-if="isEmployee" -->
      <van-button size="small" @click.native="handleSoToPo">
        create_purchase
      </van-button>
    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant'

export default {
  name: 'SurveyInput',
  components: { [Dialog.Component.name]: Dialog.Component },
  directives: {},
  filters: {},
  props: {},

  data() {
    return {
      //
    }
  },
  computed: {
    isCustomer: function() {
      return !this.$store.state.user.isEmployee
    },
    isEmployee: function() {
      return this.$store.state.user.isEmployee
    },

    isInvoiceToPay: function() {
      const invoice = this.invoice
      return (
        invoice.state === 'posted' &&
        invoice.invoice_payment_state === 'not_paid'
      )
    },

    isSoToDone: function() {
      const so = this.so
      const invoice = this.invoice
      return (
        so.state === 'sale' &&
        so.invoice_status === 'invoiced' &&
        invoice.state === 'posted' &&
        invoice.invoice_payment_state === 'paid'
      )
    },

    invoice: function() {
      const inv = this.$store.state.AccountMove.dataDict
      const states = { draft: '草稿', posted: '已确认' }

      const invoice_payment_states = {
        not_paid: '未付款',
        paid: '已支付'
      }

      return {
        ...inv,
        state_name: states[inv.state] || inv.state,
        invoice_payment_state_name:
          invoice_payment_states[inv.invoice_payment_state] ||
          inv.invoice_payment_state
      }
    },

    so: function() {
      const so = this.$store.state.SaleOrder.dataDict
      const states = { draft: '草稿', sale: '确认', done: '已完成' }
      const invoice_statuses = {
        no: '无',
        'to invoice': '待开票',
        invoiced: '已开票'
      }

      return {
        ...so,
        id: so.id,
        analytic_account_partner_id__name: so.analytic_account_partner_id__name,
        state_name: states[so.state] || so.state,
        invoice_status_name:
          invoice_statuses[so.invoice_status] || so.invoice_status
      }
    }
  },
  async created() {
    const { id } = this.$route.query
    await this.$store.dispatch('SaleOrder/browse_one', { id })

    console.log('SaleOrder,', this.so)
    console.log('Invoice,', this.invoice)
  },

  methods: {
    async handleSoConfirm() {
      console.log('handleSoConfirm,', this.so.state)
      if (this.so.state === 'draft') {
        await this.$store.dispatch('SaleOrder/action_confirm', {
          id: this.so.id
        })
      }
    },

    async handleSoToInvoice() {
      console.log('handleSoToInvoice,', this.so.invoice_status)
      if (this.so.invoice_status === 'to invoice') {
        await this.$store.dispatch('SaleOrder/create_invoices', {
          id: this.so.id
        })
      }
    },

    async handleInvoicePost() {
      console.log('handleInvoicePost,', this.invoice.state)
      if (this.invoice.state === 'draft') {
        await this.$store.dispatch('AccountMove/action_post', {
          id: this.invoice.id
        })
      }
    },

    async handleInvoicePay() {
      console.log('handleInvoicePost,', this.invoice.state)
      if (this.isInvoiceToPay) {
        Dialog.confirm({
          title: '支付',
          message: '去往第三方支付?',
          // eslint-disable-next-line space-before-function-paren
          beforeClose: async (action, done) => {
            if (action === 'confirm') {
              // setTimeout(done, 1000)
              await this.$store.dispatch('AccountMove/create_payments', {
                id: this.invoice.id
              })
              done()
            } else {
              done()
            }
          }
        })
      }
    },

    async handleSoDone() {
      console.log('handleSoDone,', this.so.state)
      if (this.isSoToDone) {
        await this.$store.dispatch('SaleOrder/action_done', {
          id: this.so.id
        })
      }
    },

    async handleSoToPo() {
      console.log('handleSoToPo,', this.so)
      // await this.$store.dispatch('PurchaseOrder/create_invoices', this.so)
      await this.$store.dispatch('PurchaseOrder/find_by_so', {
        ...this.so
      })

      const po = this.$store.state.PurchaseOrder.dataDict
      // this.$router.push({
      //   path: '/po/po',
      //   query: { id: po.id }
      // })
      console.log('po', po)
    }
  }
}
</script>

<style type="text/css"></style>
